const {defineConfig} = require('@vue/cli-service')
const path = require("path");
const resolve = (dir) => path.join(__dirname, dir);
const CompressionWebpackPlugin = require("compression-webpack-plugin"); // 开启gzip压缩， 按需引用
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩， 按需写入
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
//压缩代码并去掉console
const TerserPlugin = require('terser-webpack-plugin');
module.exports = defineConfig({
    publicPath: "./",
    indexPath: "index.html", // 相对于打包路径index.html的路径
    transpileDependencies: true,
    assetsDir: 'static',
    productionSourceMap: false,
    chainWebpack: config => {
        config.resolve.symlinks(true); // 修复热更新失效
        config.plugin("html").tap((args) => {
            // 修复 Lazy loading routes Error
            args[0].title = 'vue-test-antd1'
            args[0].chunksSortMode = "none";
            return args;
        })
        config.resolve.alias
            .set('@', resolve('src'))
            .set('@assets', resolve('src/assets'))
            .set('@components', resolve('src/components'))
    },
    configureWebpack: config => {
        // 开启 gzip 压缩
        // 需要 npm i -D compression-webpack-plugin
        const plugins = [];
        if (IS_PROD) {
            plugins.push(
                new CompressionWebpackPlugin({
                    filename: "[path][base].gz",
                    algorithm: "gzip",
                    test: productionGzipExtensions,
                    threshold: 10240,
                    minRatio: 0.8
                })
            );
            //启用代码压缩
            plugins.push(
                new TerserPlugin({
                    terserOptions: {
                        ecma: undefined,
                        warnings: false,
                        parse: {},
                        compress: {
                            drop_console: true,
                            drop_debugger: false,
                            pure_funcs: ['console.log'] // 移除console
                        }
                    },
                })
            );
        }
        config.plugins = [...config.plugins, ...plugins];
    },
    css: {
      loaderOptions: {
        less: {
          // 自定义主题样式
        // modifyVars: {
        //   'primary-color': '#41B883',
        //   'link-color': '#41B883',
        //   'border-radius-base': '2px'
        // }
          // 解决问题主要需要打开这个
          javascriptEnabled: true
        }
      }
    },
    devServer: {
        client: {
            overlay: {
                warnings: true,
                errors: true,
            },
        },
        host: "localhost",
        port: 8040, // 端口号
        https: false, // https:{type:Boolean}
        open: false, //配置自动启动浏览器
        hot: true, // 热更新
    }
})
